﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using InsuranceCompany.Helpers;
using InsuranceType = InsuranceCompany.Core.Domain.InsuranceArea.InsuranceType;
using InsuranceRisks = InsuranceCompany.Core.Domain.InsuranceArea.InsuranceRisks;
using InsuranceRisk = InsuranceCompany.Core.Domain.InsuranceArea.InsuranceRisk;

namespace InsuranceCompany.Categories
{
    public partial class Edit : InsuranceForm, InsuranceEditModuleInterface
    {
        private InsuranceType IType;
        private InsuranceRisks IRisksAll;
        private InsuranceRisk SelectedIRisk;

        public Edit()
        {
            InitializeComponent();
        }
        public Edit(ref List<ObjectsCollection> objects)
        {
            InitializeComponent();
            this.Objects = objects;
            this.IRisksAll = new InsuranceRisks();
        }
        public Edit(ref List<ObjectsCollection> objects, InsuranceType itype)
        {
            InitializeComponent();
            this.Objects = objects;
            this.IType = itype;
            this.IRisksAll = itype.GetRisks();
            this.textBoxName.Text = itype.Name;
            this.textBoxRiskMaxSum.Text = itype.RiskMaxSum.ToString();
            this.textBoxPriceMinPayment.Text = itype.MinCost.ToString();
            this.textBoxMinContractTime.Text = itype.MinContractTime.ToString();
            this.textBoxMaxContractTime.Text = itype.MaxContractTime.ToString();
            this.UpdateItemsListView();
        }   
        private void SaveItem(InsuranceType itype)
        {
            if (FieldsValidation.IsValid())
            {
                ObjectsCollection itypes = this.Objects.Find(x => x.GetName() == "PolicyTypes");
                if (this.IType == null)
                {
                    this.IType = itype;
                    itypes.Items.Add(itype);
                }
                else
                {
                    itypes.Items.Remove(this.IType);
                    this.IType = itype;
                    itypes.Items.Add(itype);
                }
                MessageBox.Show("Данные сохранены");
            }
        }
        private void SaveButtonClick(object sender, EventArgs e)
        {
            InsuranceType itype = new InsuranceType();
            itype.Name = this.textBoxName.Text;
            if (this.textBoxRiskMaxSum.Text == "")
                this.textBoxRiskMaxSum.Text = "0";
            itype.RiskMaxSum = Decimal.Parse(this.textBoxRiskMaxSum.Text);
            if (this.textBoxPriceMinPayment.Text == "")
                this.textBoxPriceMinPayment.Text = "0";
            itype.MinCost = Decimal.Parse(this.textBoxPriceMinPayment.Text);
            if (this.textBoxMinContractTime.Text == "")
                this.textBoxMinContractTime.Text = "0";
            itype.MinContractTime = Int32.Parse(this.textBoxMinContractTime.Text);
            if (this.textBoxMaxContractTime.Text == "")
                this.textBoxMaxContractTime.Text = "0";
            itype.MaxContractTime = Int32.Parse(this.textBoxMaxContractTime.Text);
            itype.AddRisks(this.IRisksAll.GetRisks());
            this.SaveItem(itype);       
        }

        private void SaveRiskButtonClick(object sender, EventArgs e)
        {
            if (this.RiskNameTextBox.Text != "" && Decimal.Parse(this.RiskSumTextBox.Text) >= 0) 
            {
                // если существует, то заменить
                this.IRisksAll.AddRisk(new InsuranceRisk(this.RiskNameTextBox.Text, this.RiskDescTextBox.Text,
                                                        Decimal.Parse(this.RiskSumTextBox.Text) ) );
                this.UpdateItemsListView();
            }
        }

        private void DeleteRiskButtonClick(object sender, EventArgs e)
        {
            this.IRisksAll.GetRisks().Remove(this.SelectedIRisk);
            this.UpdateItemsListView();
        }

        private void RisksListViewItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            if (this.RisksListView.SelectedItems.Count == 1)
            {
                ListView.SelectedListViewItemCollection selectedItem = this.RisksListView.SelectedItems;
                string riskName = "";
                foreach (ListViewItem item in selectedItem)
                {
                    riskName = item.SubItems[0].Text;
                    this.RiskNameTextBox.Text = item.SubItems[0].Text;
                    this.RiskSumTextBox.Text = item.SubItems[1].Text;
                    this.RiskDescTextBox.Text = item.SubItems[2].Text;
                    break;
                }
                this.SelectedIRisk = this.IRisksAll.GetRisks().Find(x => x.Name == riskName);
            }
        }
        private void UpdateItemsListView()
        {
            this.RisksListView.Items.Clear();
            foreach (InsuranceRisk item in this.IRisksAll.GetRisks())
            {
                ListViewItem listViewItem = new ListViewItem();
                listViewItem.SubItems[0].Text = item.Name;
                listViewItem.SubItems.Add(item.Sum.ToString());
                listViewItem.SubItems.Add(item.Description);              
                this.RisksListView.Items.Add(listViewItem);
            }
        }

        protected void CloseClick(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
